package com.three.DAO.impl;

import com.three.DAO.BaseDAO;
import com.three.DAO.SceneDAO;
import com.three.entity.Scene;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SceneDAOImpl extends BaseDAO implements SceneDAO {

    ResultSet rs=null;

    @Override
    public int addScene() {
        return 0;
    }

    @Override
    public int addPrice(Scene scene) {
        String sql="update scene set price=? where id=?";
        Object[] values={scene.getPrice(),scene.getId()};
        int count=super.exeUpdate(sql,values);
        return count;
    }

    @Override
    public Scene selectSenceById(Scene scene) {
        String sql="select * from scene where id=?";
        Object[] values={scene.getId()};
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                scene.setType(rs.getString("type"));
                scene.setData(rs.getInt("data"));
                scene.setDescription(rs.getString("description"));
                scene.setPrice(rs.getDouble("price"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try{
                    if(rs!=null){
                        rs.close();
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return scene;
    }

    @Override
    public List<Scene> getAllSecne() {
        Scene scene=null;
        List<Scene> list=new ArrayList<>();
        String sql="select * from scene";
        Object[] values={};
        rs=super.exeQuery(sql,values);
        try{
            while(rs.next()){
                scene=new Scene();
                scene.setId(rs.getInt("id"));
                scene.setType(rs.getString("type"));
                scene.setData(rs.getInt("data"));
                scene.setDescription(rs.getString("description"));
                scene.setPrice(rs.getDouble("price"));
                list.add(scene);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try{
                    if(rs!=null){
                        rs.close();
                    }
                }catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }
}
